﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media.Animation;
using System.Drawing;
using System.Diagnostics;

namespace AutoClick.WPFHelpers
{
    /// <summary>
    /// Class proposing controls for the Circle reference of autoclicks
    /// </summary>
    public class AutoClickCircleControl
    {
        #region Variables
        private bool _circleStarted;
        private bool _showCircle;
        private bool _autoClickNeeded;
        private WPFAutoClickCircle _wpfAutoClickCircle;
        public bool ShowCircle
        {
            get { return _showCircle; }
            set { _showCircle = value; }
        }
        public bool Started
        {
            get { return _circleStarted; }
            set { _circleStarted = value; }
        }
        public bool AutoClickNeeded
        {
            get { return _autoClickNeeded; }
            set { _autoClickNeeded = value; }
        }
        public WPFAutoClickCircle AutoClickCircle
        {
            get { return _wpfAutoClickCircle; }
            set { _wpfAutoClickCircle = value; }
        }
        #endregion

        #region Constructor
        public AutoClickCircleControl(bool autoClickNeeded)
        {
            //Initialize the circle
            AutoClickNeeded = autoClickNeeded;
            _wpfAutoClickCircle = new WPFAutoClickCircle();
            _wpfAutoClickCircle.Topmost = true;
            //Set animation
            Storyboard wpfCircleGlowingStoryboard = (Storyboard)_wpfAutoClickCircle.FindResource("WPFCircleGlowingStoryboard");
            wpfCircleGlowingStoryboard.Begin();
        }
        #endregion

        #region Interactions's circle
        /// <summary>
        /// Method that show the circle displays the area within the pointer must stay if the user wants a click to be sent.
        /// </summary>
        public void Start(int diameter)
        {
            if (!Started && AutoClickNeeded)
            {
                _wpfAutoClickCircle.Width = diameter;
                _wpfAutoClickCircle.Height = diameter;
                _wpfAutoClickCircle.Visibility = System.Windows.Visibility.Visible;
                Started = true;
            }
        }
        /// <summary>
        /// Method that hide the circle displays the area within the pointer must stay if the user wants a click to be sent.
        /// </summary>
        public void Stop()
        {
            if (Started)
            {
                _wpfAutoClickCircle.Visibility = System.Windows.Visibility.Collapsed;
                Started = false;
            }
        }
        /// <summary>
        /// Method that moves the circle to the current Pointer Location if the circle is required.
        /// </summary>
        public void UpdateCirclePosition(Point referencialMousePosition)
        {
            if (_wpfAutoClickCircle != null && referencialMousePosition != null && ShowCircle && AutoClickNeeded)
            {
                _wpfAutoClickCircle.Left = referencialMousePosition.X - (_wpfAutoClickCircle.ActualWidth / 2);
                _wpfAutoClickCircle.Top = referencialMousePosition.Y - (_wpfAutoClickCircle.ActualHeight / 2);
            }
        }

        /// <summary>
        /// Method that resize the circle to the current radius and update the position if the circle is required.
        /// </summary>
        public void UpdateCircleRadius(int diameter, Point referencialMousePosition)
        {
            if (_wpfAutoClickCircle != null && ShowCircle && AutoClickNeeded)
            {
                _wpfAutoClickCircle.Width = diameter;
                _wpfAutoClickCircle.Height = diameter;
                UpdateCirclePosition(referencialMousePosition);
            }
        }
        #endregion
    }
}
